package com.h3bpm.handler;

import OThinker.Common.Data.Database.ICommand;
import OThinker.Common.Organization.Models.User;
import OThinker.H3.Entity.IEngine;
import OThinker.H3.Entity.WorkItem.WorkItemModels.WorkItem;
import data.DataRow;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/*
  存储返回实例ID 代办 已办
   WJQ
 */
public class OrcaleSelectHelper {
    private static final Logger logger = LoggerFactory.getLogger(OrcaleSelectHelper.class);




    //存储数据库数据  实例
    public static Boolean storageProtalInstanceId(String instancId, String instanceName,
                                                  String originatorName,String bizObjectId,String bizObjectSchemaCode,
                                                  String originator, IEngine engine) {
        try {
            String objectId = UUID.randomUUID().toString();
            String selectSql = "";
            ICommand command = engine.getEngineConfig().getCommandFactory().CreateCommand();
            if (instancId != null && !"".equals(instancId)) {
                selectSql = "INSERT INTO E_INSTANCE VALUES('"+objectId+"','"+instancId+"','"+instanceName+"',NOW(),'"+originatorName+"','"+bizObjectId+"','"+bizObjectSchemaCode+"','0','"+originator+"')";
                logger.info("存储实例————————instance——————"+instancId);
                int res =command.ExecuteNonQuery(selectSql);
                if(res>0){
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  false;
    }
    // 存储更新实例
    public static Boolean createProtalUpdateInstance(IEngine engine,String instance,String state) {
        try {

            String objectId = UUID.randomUUID().toString();
            String selectSql = "";
            logger.info("存储更新实例————————instance_id——————"+instance);
            ICommand command = engine.getEngineConfig().getCommandFactory().CreateCommand();
            if (instance != null && !"".equals(instance)) {
                selectSql = "INSERT INTO E_UPDATEINSTANCE VALUES('"+objectId+"','"+instance+"',NOW(),'"+state+"','0')";
                int res =command.ExecuteNonQuery(selectSql);
                if(res>0){
                    logger.info("存储更新实例——————————————success");
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  false;

    }

    /**
     *
     *@describe  查询流程是否已存在
     *@params [instanceId, engine]
     *@return java.lang.Boolean
     *@author VillWang
     *@date 2020/5/29
     *@other
     *
     */
    public static Boolean selectProtalInstanceId(String instanceId, IEngine engine) {

        try {

            logger.info("查询流程是否已存在————————instance_id——————"+instanceId);

            ICommand command = engine.getEngineConfig().getCommandFactory().CreateCommand();
            if (instanceId != null && !"".equals(instanceId)) {
                String sql=" select objectId from e_instance where instanceId='"+instanceId+"'";
                DataTable table = command.ExecuteDataTable(sql);
                if (table != null || !"".equals(table)) {
                    List<DataRow> entityRows = table.getEntityRows();
                    if (!entityRows.isEmpty() && entityRows.size() > 0) {
                        return  false;
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  true;
    }
    //存储数据库数据  代办
    public static Boolean storageProtalTask(IEngine engine, HashMap<String, Object> map) {
        try {

            String objectId = UUID.randomUUID().toString();
            String instance= (String) map.get("instance");
            String displayName= (String) map.get("displayName");
            String participant= (String) map.get("participant");
            String participantId= (String) map.get("participantId");
            String url= (String) map.get("url");
            String h5url= (String) map.get("h5url");
            String taskId= (String) map.get("taskId");
            String selectSql = "";
            logger.info("存储代办————————taskId——————"+taskId);
            ICommand command = engine.getEngineConfig().getCommandFactory().CreateCommand();
            if (instance != null && !"".equals(instance)) {
                selectSql = "INSERT INTO E_TASK VALUES('"+objectId+"','"+instance+"','"+url+"','"+h5url+"',NOW(),'0','"+displayName+"','"+participant+"','"+participantId+"','"+taskId+"')";
                logger.info("存储代办————————instance——————"+instance);
                int res =command.ExecuteNonQuery(selectSql);
                if(res>0){
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  false;

    }

    //添加短信信息
    public static Boolean backwardSendMessage(IEngine engine, WorkItem item) {
        String sql = "select OriginatorName,InstanceName from ot_instancecontext where ObjectID='%s'";
        try {
            ICommand command = engine.getEngineConfig().getCommandFactory().CreateCommand();
            String mobile = ((User) engine.getOrganization().GetUnit(item.getParticipant())).getMobile();
            DataTable table = engine.getQuery().QueryTable(String.format(sql, item.getInstanceID()));
            List<DataRow> entityRows = table.getEntityRows();
            if (!entityRows.isEmpty() && entityRows.size() > 0) {
                String originatorName = entityRows.get(0).getString("OriginatorName");
                String instanceName = entityRows.get(0).getString("InstanceName");
                String insertSql = "insert into tbl_sms (ObjectID,Number,Message,SendFlag,CreatedTime) values (UUID(),'%s','%s','0',NOW())";
                int res = command.ExecuteNonQuery(String.format(insertSql, mobile, originatorName + "发起的流程:" + instanceName + "被驳回，请尽快处理"));
                if (res > 0) {
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
